package com.game.graphics;


import java.awt.Image;
import java.awt.Rectangle;

public class Sprite implements Comparable<Sprite>{
	
	public static final int Z_BACKGROUND=0;
	public static final int Z_BACKGROUND_RENDER=1;	
	public static final int Z_ACTIVE_DOWN=9;
	public static final int Z_ACTIVE=10;
	public static final int Z_ACTIVE_UP=11;
	public static final int Z_FORWEGROUND=1000;
	public static final int Z_FORWEGROUND_RENDER=1001;
	
    protected Animation anim;
    // position (pixels)
    private float x;
    private float y;
    // velocity (pixels per millisecond)
    private float dx;
    private float dy;
    
    private int z_index;

    /**
        Creates a new Sprite object with the specified Animation.
    */
    public Sprite(Animation anim) {
        this(anim, Z_ACTIVE);
    }
    
    public Sprite() {
    	this.z_index=Z_ACTIVE;
    }
    
    public Sprite(Animation anim,int z_index) {
    	this.anim = anim;
    	this.z_index=z_index;
    }

    /**
        Updates this Sprite's Animation and its position based
        on the velocity.
    */
    public void update(long elapsedTime) {
//        x += dx * elapsedTime;
//        y += dy * elapsedTime;
        anim.update(elapsedTime);
    }
    
    public void start(){
    	anim.start();
    }
    
    /**
        Gets this Sprite's current x position.
    */
    public float getX() {
        return x;
    }

    /**
        Gets this Sprite's current y position.
    */
    public float getY() {
        return y;
    }

    /**
        Sets this Sprite's current x position.
    */
    public void setX(float x) {
        this.x = x;
    }

    /**
        Sets this Sprite's current y position.
    */
    public void setY(float y) {
        this.y = y;
    }

    /**
        Gets this Sprite's width, based on the size of the
        current image.
    */
    public int getWidth() {
        return anim.getImage()[0].getWidth(null);
    }

    /**
        Gets this Sprite's height, based on the size of the
        current image.
    */
    public int getHeight() {
        return anim.getImage()[0].getHeight(null);
    }

    /**
        Gets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityX() {
        return dx;
    }

    /**
        Gets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public float getVelocityY() {
        return dy;
    }

    /**
        Sets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityX(float dx) {
        this.dx = dx;
    }

    /**
        Sets the vertical velocity of this Sprite in pixels
        per millisecond.
    */
    public void setVelocityY(float dy) {
        this.dy = dy;
    }
    
    /**
     * @param z_index
     */
    protected void setZ_index(int z_index) {
		this.z_index = z_index;
	} 
    
    /**
     * @return
     */
    public int getZ_index() {
		return z_index;
	}
    
    /**
        Gets this Sprite's current image.
    */
    public Image[] getImage() {
        return anim.getImage();
    }
    public MyImage getMyImage() {
    	return anim.getMyImage();
    }
    
    public Rectangle getRect() {
    	MyImage myImage=anim.getMyImage();
    	return new Rectangle((int)getX(),(int)getY(), myImage.width, myImage.height);
    }

    /**
        Clones this Sprite. Does not clone position or velocity
        info.
    */
    public Object clone() {
        return new Sprite(anim,z_index);
    }

	@Override
	public int compareTo(Sprite o) {
		if(o==null)return 0;
		return z_index-o.z_index;
	}
	
	public Animation getAnim() {
		return anim;
	}
}
